<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class template_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        if (isset($_GET['path'])) {
            $path = str_replace('.', '/', $_GET['path']);
            $pre = $_GET['path'] . '.';
            $dir = "home/templates/" . cfg('site_templates') . '/' . $path;
        } else {
            $dir = "home/templates/" . cfg('site_templates');
            $pre = '';
        }
        if (file_exists($dir)) {
            $file = scandir($dir, 1);
            for ($i = 0; $i < count($file); $i++) {
                $row['size'] = format_bytes(@filesize($dir . '/' . $file[$i]));
                $row['time'] = @filemtime($dir . '/' . $file[$i]);
                $row['path'] = $pre . $file[$i];
                $row['name'] = $file[$i];
                if (substr($file[$i], -5) == '.html' || substr($file[$i], -4) == '.htm') {
                    $file_name[] = $row;
                } else {
                    if ($file[$i] != '.' && $file[$i] != '..' && $file[$i] != 'images') {
                        $path_name[] = $row;
                    }
                }
            }
        }
        @sort($path_name);
        @sort($file_name);
        $this->assign('path_name', $path_name);
        $this->assign('file_name', $file_name);
        $this->display();
    }
    public function edit()
    {
        $path = isset($_GET['path']) ? $_GET['path'] : exit('FILE IS NULL');
        $arr = explode('.', $path);
        $filename = $arr[count($arr) - 2] . '.' . $arr[count($arr) - 1];
        $new_path = '';
        for ($i = 0; $i < count($arr); $i++) {
            if ($i == count($arr) - 2 || $i == count($arr) - 1) {
            } else {
                $new_path .= $arr[$i] . '/';
            }
        }
        $file = 'home/templates/' . cfg('site_templates') . '/' . $new_path . $filename;
        $row['filename'] = $file;
        $row['content'] = file_get_contents($file);
        $this->assign('row', $row);
        $this->display();
    }
    public function save()
    {
        if ($_POST) {
            $file = P('filename', '修改的文件名不能为空');
            $content = P('content');
            $query = file_put_contents($file, stripslashes_deep($content));
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    function is_empty_dir($fp)
    {
        $H = @opendir($fp);
        $i = 0;
        while ($_file = readdir($H)) {
            $i++;
        }
        closedir($H);
        if ($i > 2) {
            return 1;
        } else {
            return 2;
        }
    }
    function theme()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $all = $this->db->get_all("select * from " . table('theme') . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($all) {
            foreach ($all as $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('theme'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function theme_edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('theme') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    function theme_save()
    {
        if ($_POST) {
            $setarr['name'] = P('name', '名称不能为空');
            $setarr['html'] = P('html', '代码不能为空');
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('theme'), $setarr, 'id =' . $id);
            } else {
                $setarr['addtime'] = time();
                $query = $this->db->add(table('theme'), $setarr);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    function theme_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('ID不能为空');
        $query = $this->db->delete(table('theme'), 'id =' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
}